package com.qiyuesuo.auth.controller;

import com.qiyuesuo.auth.entity.User;
import com.qiyuesuo.auth.service.UserService;
import com.qiyuesuo.auth.vo.UserVo;
import com.qiyuesuo.util.JwtHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author： xulinbo
 * @Description：
 * @Date： 16:29 2022/4/26
 **/
@RestController
@RequestMapping("/auth")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public String register(@RequestBody UserVo user) {
        String message = userService.registerUser(user);
        return message;
    }

    @RequestMapping("/login")
    public String login(@RequestBody UserVo userVo){
        User user = userService.login(userVo);
        if (user != null){
            String token = JwtHelper.createToken(userVo.getName());
            return token;
        }
        return "";
    }
}
